<?php
require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';
$orm = new ORM();
$orm2 = new ORM('localhost', $orm->get_db() . '2', 'root', '911', 'latin1');
$table_prefix = 'pb_ht_';

$attr = 'Tables_in_' . $orm2->get_db();
$tstmt = $orm2->prepare('SHOW TABLES');
$tstmt->execute();
$data = new stdClass();
$i = 0;
while ($i < $tstmt->rowCount()) {
  $tname = $tstmt->fetch(PDO::FETCH_OBJ)->$attr;
  $sql = 'DROP TABLE IF EXISTS `' . $orm2->get_db() . '`.`' . $tname . '`';
  $orm2->exec($sql);
  $i++;
};

try {
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "admin`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `account` varchar(64) DEFAULT '',
           `password` varchar(64) DEFAULT '',
           `email` varchar(256) DEFAULT '',
           `shop_name` varchar(256) DEFAULT '',
           `contact` text DEFAULT NULL,
           `gmap` varchar(512) DEFAULT '',
           `footer` text DEFAULT NULL
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "customer`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `track_id` varchar(16) UNIQUE DEFAULT '',
           `account` varchar(64) UNIQUE DEFAULT '',
           `password` varchar(64) DEFAULT '',
           `salt` varchar(64) DEFAULT '',
           `reset_key` varchar(64) DEFAULT '',
           `reset_exp` datetime DEFAULT '0000-00-00 00:00:00',
           `reg_date` date DEFAULT '0000-00-00',
           `first_name` varchar(64) DEFAULT '',
           `last_name` varchar(64) DEFAULT '',
           `living_country` varchar(64) DEFAULT '',
           `living_area` varchar(64) DEFAULT '',
           `family_income` varchar(64) DEFAULT '',
           `investment_type` varchar(64) DEFAULT '',
           `interested_area` varchar(64) DEFAULT '',
           `interested_area_de` varchar(64) DEFAULT '',
           `property_type` varchar(64) DEFAULT '',
           `bedroom_num` varchar(64) DEFAULT '',
           `price_range` varchar(64) DEFAULT '',
           `email_required` tinyint(1) DEFAULT 1,
           `status` tinyint(1) DEFAULT 0,
           `ip` varchar(64) DEFAULT '',
           `ip_place` varchar(64) DEFAULT ''
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "category`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `name` varchar(256) DEFAULT '',
           `parent_id` int DEFAULT 0,
           `order` double DEFAULT 0,
           `page_id` int DEFAULT 0,
           `ad_top` varchar(256) DEFAULT '',
           `ad_bottom` varchar(256) DEFAULT '',
           `ad_right` text DEFAULT NULL,
           `visited__` int DEFAULT 0
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "article`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `track_id` varchar(16) UNIQUE DEFAULT '',
           `category_id` int DEFAULT 0,
           `categories__` varchar (512) DEFAULT '',
           `column__` varchar (512) DEFAULT '',
           `full_column__` varchar (512) DEFAULT '',
           `title` varchar(512) DEFAULT '',
           `content` text DEFAULT NULL,
           `date` datetime DEFAULT '0000-00-00 00:00:00',
           `visited__` int DEFAULT 0
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "article_file`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `article_track_id` varchar(16) DEFAULT '',
           `name` varchar(64) DEFAULT '',
           `link` varchar(512) DEFAULT '',
           `is_image` tinyint(1) DEFAULT 0
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "hm_data`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `ad_top` text DEFAULT NULL,
           `ad_middle` text DEFAULT NULL,
           `ad_bottom` text DEFAULT NULL,
           `cats1` varchar(512) DEFAULT '',
           `cats2` varchar(512) DEFAULT ''
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "hm_image`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `name` varchar(64) DEFAULT '',
           `link` varchar(512) DEFAULT ''
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
  
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_prefix . "newsletter`(
           `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
           `search` varchar(1024) DEFAULT '',
           `subject` varchar(512) DEFAULT '',
           `content` text DEFAULT NULL,
           `sent` datetime DEFAULT '0000-00-00 00:00:00'
          ) ENGINE=MYISAM;";
  $orm2->exec($sql);
//  
//  $orm->exec("INSERT INTO " . $table_prefix . "admin values (NULL, 'admin', '" . md5(123456) . "', '', '', '', '', '', '')");
} catch (PDOException $e) {
  Utils::dump($e);
}
?>